package cn.com.generaldata.os.service.indicator_evaluation.incr_analysis;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 海洋生产总值可比价评估指标计算类.
 */
public class GOPComparablePriceCalculateService extends IndicatorEvaluationCalculateService {

	/** 地区生产总值 */
	private String GDPDeflatorIndicatorName = "地区生产总值平减指数";

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;
	}

	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {

		Double GOP = getGOP(indicatorAccountVO);
		Validate.isTrue(GOP != null);

		Double GDPDeflator = getGDPDeflator(indicatorAccountVO);
		Validate.isTrue(GDPDeflator != null, "地区生产总值平减指数，不可以计算");

		return GOP / GDPDeflator;
	}

	private Double getGOP(IndicatorAccountVO indicatorAccountVO) {
		return getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				ProvinceIndicatorConstant.GOP_INDICATOR_NAME);
	}

	private Double getGDPDeflator(IndicatorAccountVO indicatorAccountVO) {
		Integer indicatorId = getIndicatorEvaluationId(GDPDeflatorIndicatorName, indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorId);
		return getEvaluationIndicatorValue(cri);
	}

}
